package com.qxzn.app.api.greendao;

import android.content.Context;

import com.qxzn.app.api.greendao.bean.DaoMaster;
import com.qxzn.app.api.greendao.bean.DaoSession;
import com.qxzn.app.util.SharePreferenceUtil;

import org.greenrobot.greendao.database.Database;

public class DbManager {

    private static DbManager dbManager;
    private static DaoSession daoSession;

    private DbManager(Context context){
        initGreenDaoDb(context);
    }

    private void initGreenDaoDb(Context context) {
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, "robot_" + SharePreferenceUtil.getUserName(context));
        Database db = helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }

    public static DbManager getInstance(Context context){
        if (dbManager == null) {
            synchronized (DbManager.class) {
                if (dbManager == null) {
                    dbManager = new DbManager(context.getApplicationContext());
                }
            }
        }
        return dbManager;
    }

    public static DaoSession getDaoSession() {
        return daoSession;
    }

    public static void setDaoSession(DaoSession daoSession) {
        DbManager.daoSession = daoSession;
    }

}
